<?php

declare(strict_types=1);

namespace DreamCat\StateMachine\Parser\Impl;

use DreamCat\StateMachine\Parser\ConfigParserInterface;
use DreamCat\StateMachine\Parser\Inner\ParseConfig;
use DreamCat\StateMachine\Popo\MachineDefine;

/**
 * 单文件配置解析类
 * @author vijay
 */
class SingleFileConfigParser implements ConfigParserInterface
{
    /** @var ParseConfig 辅助类 */
    private ParseConfig $parseConfig;

    /**
     * @param string $definePath 要解析的配置文件路径
     */
    public function __construct(private string $definePath)
    {
        $this->parseConfig = new ParseConfig();
    }

    /** @inheritDoc */
    public function parseConfig(): MachineDefine
    {
        $config = $this->parseConfig->getJsonFile($this->definePath, "状态机配置文件");
        $defDir = $this->parseConfig->getSchemaDir();
        $struct = $this->parseConfig->getJsonFile("{$defDir}/StateMachineStructDefine.json", "状态机结构定义");
        $this->parseConfig->validSchema($config, $struct, "状态机配置文件");
        # 解析通过，拆解到定义popo中
        return $this->parseConfig->checkParseConfig($config);
    }
}

# end of file
